/// 防止重复跳转token
let routerToken = 0;
/// 最大push深度
const jumpDeepLimit = 10;

/**
 * @auth chenkefeng@kuaicto.com
 * 
 * 跳转到指定界面， push 或者 redirect
 * 防止多次跳转
 * 
 * @param url 
 *        指定界面
 * @param params 
 *        参数 Object类型
 * @param isRedirect
 *        是否是redirect方式跳转，默认false
 */
export const jumpTo = (url, params = {}, isRedirect = false) => {
  let urlComps = url.split('?');
  if (!urlComps || urlComps.length == 0) {
    return;
  }
  if (routerToken > 0) {
    return;
  }
  routerToken = 1;

  let pagePath = urlComps[0];
  let queryPath = '?';
  if (urlComps.length > 1) {
    queryPath = `?${urlComps[1]}`;
  }
  if (queryPath.length > 1 && !queryPath.endsWith("&")) {
    queryPath = `${queryPath}&`;
  }
  for (let key in params) {
    if (params[key] !== undefined) {
      queryPath += `${key}=${params[key]}&`;
    }
  }
  queryPath = queryPath.substring(0, queryPath.length - 1);
  pagePath += queryPath;
  if (wx.profileEnv !== 'prod'){
    console.log(`界面跳转目标：${pagePath}`);
  }
  let pathList = getCurrentPages() || [];
  if (pathList.length < jumpDeepLimit && !isRedirect) {
    wx.navigateTo({
      url: pagePath,
      complete: function () {
        routerToken = 0;
      }
    });
  } else {
    wx.redirectTo({
      url: pagePath,
      complete: function () {
        routerToken = 0;
      }
    });
  }
}